package com.squirrel.michale;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/18 7:52 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode1138 {

    public String alphabetBoardPath2(String target) {
        char[] chars = target.toCharArray();
        int currentRow = 0;
        int currentColum = 0;
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            int boardIndex = chars[i] - 'a';
            int row = boardIndex / 5;
            int colum = boardIndex % 5;
            if (row == currentRow && colum == currentColum) {
                stringBuffer.append("!");
                currentRow = row;
                currentColum = colum;
                continue;
            }
            if(row == 5&&currentRow!=5){
                if (colum > currentColum) {
                    for (int j = 0; j < colum - currentColum; j++) {
                        stringBuffer.append("R");
                    }
                } else if (colum < currentColum) {
                    for (int j = 0; j < currentColum - colum; j++) {
                        stringBuffer.append("L");
                    }
                }

                if (row > currentRow) {
                    for (int j = 0; j < row - currentRow; j++) {
                        stringBuffer.append("D");
                    }
                    stringBuffer.append("!");
                } else if (row < currentRow) {
                    for (int j = 0; j < currentRow - row; j++) {
                        stringBuffer.append("U");
                    }
                    stringBuffer.append("!");
                }else if (row == currentRow) {
                    stringBuffer.append("!");
                }
            }else {
                if (row > currentRow) {
                    for (int j = 0; j < row - currentRow; j++) {
                        stringBuffer.append("D");
                    }
                } else if (row < currentRow) {
                    for (int j = 0; j < currentRow - row; j++) {
                        stringBuffer.append("U");
                    }
                }

                if (colum > currentColum) {
                    for (int j = 0; j < colum - currentColum; j++) {
                        stringBuffer.append("R");
                    }
                    stringBuffer.append("!");
                } else if (colum < currentColum) {
                    for (int j = 0; j < currentColum - colum; j++) {
                        stringBuffer.append("L");
                    }
                    stringBuffer.append("!");
                } else if (colum == currentColum) {
                    stringBuffer.append("!");
                }
            }
            currentRow = row;
            currentColum = colum;
        }
        return stringBuffer.toString();
    }



    public String alphabetBoardPath(String target) {
        int currentRow = 0;
        int currentColum = 0;
        StringBuffer stringBuffer = new StringBuffer();
        for (char alphabet : target.toCharArray()) {
            int boardIndex = alphabet - 'a';
            int row = boardIndex / 5;
            int colum = boardIndex % 5;
            // 顺序很重要，比如target = "zigzag"中z->i要先Up，g->z要先Left  "左上右下"
            if (colum < currentColum) {
                for (int j = 0; j < currentColum - colum; j++) {
                    stringBuffer.append("L");
                }
            }
            if (row < currentRow) {
                for (int j = 0; j < currentRow - row; j++) {
                    stringBuffer.append("U");
                }
            }
            if (colum > currentColum) {
                for (int j = 0; j < colum - currentColum; j++) {
                    stringBuffer.append("R");
                }
            }
            if (row > currentRow) {
                for (int j = 0; j < row - currentRow; j++) {
                    stringBuffer.append("D");
                }
            }
            stringBuffer.append("!");
            currentRow = row;
            currentColum = colum;
        }
        return stringBuffer.toString();
    }


    public static void main(String[] args) {
//        输入：target = "leet"
//        输出："DDR!UURRR!!DDD!"
//        示例 2：
//
//        输入：target = "code"
//        输出："RR!DDRR!UUL!R!"

        LeetCode1138 leetCode1138 = new LeetCode1138();

        String target = "leet";
        System.out.println(leetCode1138.alphabetBoardPath(target));


        String target2 = "code";
        System.out.println(leetCode1138.alphabetBoardPath(target2));


//        DDRUURRR!DDD
//        RRDDRRUULR


//        DDR!UURRR!!DDD
//        RR!DDRR!UUL!R!


        String target3 = "zigzag";
        System.out.println(leetCode1138.alphabetBoardPath(target3));
    }

}
